﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class bas_role : BasePermissionPage
{
    SqlDataSource SDS_ROLE = new SqlDataSource();
    DataView dv = new DataView();
    protected void Page_Load(object sender, EventArgs e)
    {
        //== 1.連結資料庫的連接字串 ConnectionString  ，事先已經寫在 Web.Config檔案裡面了==
        SDS_ROLE.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SDS_ROLE.ProviderName = "Oracle.DataAccess.Client";
        SDS_ROLE.CancelSelectOnNullParameter = false;
        if (!IsPostBack)
        {
            //DBINIT();
        }
        else
        {

        }
    }
    protected void DBINIT()
    {
        try
        {
            string v_ROLE_ID, v_SQL, v_ROLE_NAME;
            DataSourceSelectArguments args = new DataSourceSelectArguments();


            v_ROLE_ID = txtROLE_ID.Text.Trim();
            v_ROLE_NAME = txtROLE_NAME.Text.Trim();
            //== 2.撰寫SQL指令 ==
            v_SQL = "SELECT ROLE_ID,ROLE_NAME,PARENT_ROLE_ID FROM MIS_ROLE_AP ";
            v_SQL = v_SQL + " Where (:ROLE_ID is null or ROLE_ID = :ROLE_ID) and (ROLE_NAME like :ROLE_NAME) ";
            v_SQL = v_SQL + " order by ROLE_ID ";
            SDS_ROLE.SelectParameters.Clear();
            SDS_ROLE.SelectCommand = v_SQL;
            SDS_ROLE.SelectParameters.Add("ROLE_ID", TypeCode.String, v_ROLE_ID);
            SDS_ROLE.SelectParameters.Add("ROLE_ID", TypeCode.String, v_ROLE_ID);
            SDS_ROLE.SelectParameters.Add("ROLE_NAME", TypeCode.String, "%" + v_ROLE_NAME + "%");

            //== 3.執行SQL指令 .select() ==
            SDS_ROLE.DataSourceMode = SqlDataSourceMode.DataSet;

            dv = (DataView)SDS_ROLE.Select(args);
            //dv.AddNew();
            DV1.DataSource = dv;
            DV1.DataBind();

        }
        catch (Exception e)
        {
            //Response.Write(e.Message);
            lbl_Message.Text = e.Message;

        }
    }
    protected void DV1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        try
        {

            DV1.EditIndex = e.NewEditIndex;
            DBINIT();
        }
        catch (Exception ex)
        {
            lbl_Message.Text = ex.Message;
        }
    }
    protected void DV1_ItemCanceling(object sender, ListViewCancelEventArgs e)
    {
        try
        {
            lbl_Message.Text = "";
            DV1.EditIndex = -1;
            DBINIT();
        }
        catch (Exception ex)
        {
            lbl_Message.Text = ex.Message;
        }
    }
    protected void DV1_ItemEditing(object sender, ListViewEditEventArgs e)
    {
        try
        {

            DV1.EditIndex = e.NewEditIndex;
            DBINIT();
        }
        catch (Exception ex)
        {
            lbl_Message.Text = ex.Message;
        }
    }
    protected void DV1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        int index;
        string v_ROLE_ID, v_ROLE_NAME, v_PARENT_ROLE_ID;
        TextBox TB = new TextBox();
        try
        {
            index = e.ItemIndex;
            v_ROLE_ID = (string)DV1.DataKeys[index].Value;
            // EmptyDataTemplate
            v_ROLE_NAME = ((TextBox)DV1.EditItem.FindControl("txtROLE_NAME_G")).Text.Trim();
            v_PARENT_ROLE_ID = ((TextBox)DV1.EditItem.FindControl("txtPARENT_ROLE_ID")).Text.Trim();
            TB = ((TextBox)DV1.EditItem.FindControl("txtPARENT_ROLE_ID"));
            lbl_Message.Text = string.Empty;
            if (check(v_PARENT_ROLE_ID) == false)
            {
                lbl_Message.Text = "上層角色代號必需存在於角色代號檔中!";
                TB.Focus();
                e.Cancel = true;
            }
            else
            {

                //'==下面的程式，只是作SQL指令的更新動作而已
                //'== 設定SQL指令將會用到的參數 ==
                SDS_ROLE.SelectParameters.Clear();
                SDS_ROLE.UpdateParameters.Add("ROLE_NAME", v_ROLE_NAME);
                SDS_ROLE.UpdateParameters.Add("PARENT_ROLE_ID", v_PARENT_ROLE_ID);
                SDS_ROLE.UpdateParameters.Add("UPD_ID", User.Identity.Name);
                SDS_ROLE.UpdateParameters.Add("UPD_DATE", System.DateTime.Now.ToString("yyyyMMdd"));
                SDS_ROLE.UpdateParameters.Add("UPD_TIME", System.DateTime.Now.ToString("HHmmss"));
                SDS_ROLE.UpdateParameters.Add("ROLE_ID", v_ROLE_ID);

                SDS_ROLE.UpdateCommand = "UPDATE MIS_ROLE_AP SET ROLE_NAME = :ROLE_NAME, PARENT_ROLE_ID = :PARENT_ROLE_ID, UPD_ID = :UPD_ID, UPD_DATE = :UPD_DATE, UPD_TIME = :UPD_TIME WHERE ROLE_ID = :ROLE_ID";
                SDS_ROLE.Update();
                //'==========================================
                //'==== 離開編輯模式 ====
                DV1.EditIndex = -1;
                DBINIT();
            }




        }
        catch (Exception ex)
        {
            lbl_Message.Text = ex.Message;
        }
    }
    protected void DV1_ItemDeleting(object sender, ListViewDeleteEventArgs e)
    {
        int index;
        string v_ROLE_ID;
        try
        {
            index = e.ItemIndex;
            v_ROLE_ID = (string)DV1.DataKeys[index].Value;

            //'==下面的程式，只是作SQL指令的更新動作而已
            //'== 設定SQL指令將會用到的參數 ==
            SDS_ROLE.DeleteParameters.Add("ROLE_ID", v_ROLE_ID);
            SDS_ROLE.DeleteCommand = "Delete MIS_ROLE_AP Where ROLE_ID = :ROLE_ID";
            SDS_ROLE.Delete();
            DBINIT();

        }
        catch (Exception ex)
        {
            lbl_Message.Text = ex.Message;
        }
    }
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        var controlItem = new List<ControlItem>() 
        {
          new ControlItem("txtROLE_ID", txtROLE_ID.Text, txtROLE_ID.GetType()),
          new ControlItem("txtROLE_NAME", txtROLE_NAME.Text, txtROLE_NAME.GetType())      
        };
        ConditionRecordUtil.RecordSearchConditionData(controlItem);
        DBINIT();
        lbl_Message.Text = string.Empty;
        int PageSize = ((DataPager)DV1.FindControl("DataPager1")).PageSize;
        ((DataPager)DV1.FindControl("DataPager1")).SetPageProperties(0, PageSize, true);
        
    }
    protected void DV1_ItemInserting(object sender, ListViewInsertEventArgs e)
    {
        string v_ROLE_ID, v_ROLE_NAME, v_PARENT_ROLE_ID;
        TextBox TB = new TextBox();
        try
        {
            v_ROLE_ID = ((TextBox)DV1.InsertItem.FindControl("txtROLE_ID_G")).Text.Trim();
            // EmptyDataTemplate
            v_ROLE_NAME = ((TextBox)DV1.InsertItem.FindControl("txtROLE_NAME_G")).Text.Trim();
            v_PARENT_ROLE_ID = ((TextBox)DV1.InsertItem.FindControl("txtPARENT_ROLE_ID")).Text.Trim();

            TB = ((TextBox)DV1.InsertItem.FindControl("txtPARENT_ROLE_ID"));
            lbl_Message.Text = string.Empty;
            if (check(v_PARENT_ROLE_ID) == false)
            {
                lbl_Message.Text = "上層角色代號必需存在於角色代號檔中!";
                TB.Focus();
                e.Cancel = true;
            }
            else
            {

                //'==下面的程式，只是作SQL指令的更新動作而已
                //'== 設定SQL指令將會用到的參數 ==
                SDS_ROLE.InsertParameters.Clear();

                SDS_ROLE.InsertParameters.Add("ROLE_ID", v_ROLE_ID);
                SDS_ROLE.InsertParameters.Add("ROLE_NAME", v_ROLE_NAME);
                SDS_ROLE.InsertParameters.Add("PARENT_ROLE_ID", v_PARENT_ROLE_ID);
                SDS_ROLE.InsertParameters.Add("CRT_ID", User.Identity.Name);
                SDS_ROLE.InsertParameters.Add("CRT_DATE", System.DateTime.Now.ToString("yyyyMMdd"));
                SDS_ROLE.InsertParameters.Add("CRT_TIME", System.DateTime.Now.ToString("HHmmss"));
                SDS_ROLE.InsertParameters.Add("UPD_ID", User.Identity.Name);
                SDS_ROLE.InsertParameters.Add("UPD_DATE", System.DateTime.Now.ToString("yyyyMMdd"));
                SDS_ROLE.InsertParameters.Add("UPD_TIME", System.DateTime.Now.ToString("HHmmss"));


                SDS_ROLE.InsertCommand = "insert into mis_ROLE_ap (ROLE_id, ROLE_name, parent_ROLE_id, crt_id, crt_date, crt_time, upd_id, upd_date, upd_time) values(:ROLE_id, :ROLE_name, :parent_ROLE_id, :crt_id, :crt_date, :crt_time, :upd_id, :upd_date, :upd_time)";
                SDS_ROLE.Insert();
                //'==========================================
                DBINIT();
            }

        }

        catch (Exception ex)
        {
            //Response.Write(ex.Message);
            //PK重覆
            if (ex.Message.IndexOf("ORA-00001") >= 0)
            {
                lbl_Message.Text = "輸入的公司代號已存在公司檔中！";
            }
            else
            {
                lbl_Message.Text = ex.Message;
            }

        }
    }

    protected void DV1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
    {
        DV1.SelectedIndex = -1;

    }
    protected void DV1_PagePropertiesChanged(object sender, EventArgs e)
    {
        int PageSize = ((DataPager)DV1.FindControl("DataPager1")).PageSize;
        int StartRowIndex = ((DataPager)DV1.FindControl("DataPager1")).StartRowIndex;
        int TotalRowCount = ((DataPager)DV1.FindControl("DataPager1")).TotalRowCount;
        ((DataPager)DV1.FindControl("DataPager1")).SetPageProperties(StartRowIndex, PageSize, true);
        DBINIT();
    }

    protected Boolean check(string p_PARENT_ROLE_ID)
    {
        //判斷PARENT_ROLE_ID必需存在ROLE_ID
        if (p_PARENT_ROLE_ID != string.Empty)
        {
            DataSourceSelectArguments args = new DataSourceSelectArguments();
            DataView DV = new DataView();

            SDS_ROLE.SelectCommand = "SELECT ROLE_ID FROM MIS_ROLE_AP Where ROLE_ID = :ROLE_ID ";
            SDS_ROLE.SelectParameters.Clear();
            SDS_ROLE.SelectParameters.Add("ROLE_ID", TypeCode.String, p_PARENT_ROLE_ID);

            //== 3.執行SQL指令 .select() ==
            SDS_ROLE.DataSourceMode = SqlDataSourceMode.DataSet;
            DV = (DataView)SDS_ROLE.Select(args);
            if (DV.Table.Rows.Count == 0)
            {
                return false;
            }

        }
        return true;
    }
    protected void btn_exit_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Default.aspx");
    }
}